﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using TekaMUD.Engine.Blocks;

namespace TekaMUD.Engine
{
	public class Locks
	{
		private static object SyncRoot = new object();

		public static readonly ReaderWriterLockSlim StorageLock = new ReaderWriterLockSlim();
		private static Dictionary<Room, Object> RoomLocks = new Dictionary<Room, object>();
		private static Dictionary<Continent, Object> ContinentLocks = new Dictionary<Continent, object>();

		/// <summary>
		/// Obtains a lock for a specific room in the game
		/// </summary>
		public static object GetRoomLock(Room r)
		{
			lock(SyncRoot)
			{
				if (RoomLocks.ContainsKey(r))
					return RoomLocks[r];

				Object o = new object();
				RoomLocks[r] = o;

				return o;					
			}
		}

		/// <summary>
		/// Obtains a lock for a specific room in the game
		/// </summary>
		public static object GetContinentLock(Continent c)
		{
			lock (SyncRoot)
			{
				if (ContinentLocks.ContainsKey(c))
					return ContinentLocks[c];

				Object o = new object();
				ContinentLocks[c] = o;

				return o;
			}
		}
	}
}
